/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.leeloo.databinding.ui.event.IEvent;
import org.leeloo.databinding.ui.event.ResumeEvent;
import org.leeloo.databinding.ui.event.SuspendEvent;

/**
 * Builds the event viewer.
 * 
 * @author Brad Reynolds
 */
public class EventViewerBuilder {
    private final static String[] columnTexts = new String[] {
            Messages.getString("EventViewerBuilder.header_type"), Messages.getString("EventViewerBuilder.header_description") }; //$NON-NLS-1$ //$NON-NLS-2$

    private final static int[] columnWeights = new int[] { 10, 70, 20 };

    private Composite parent;

    public EventViewerBuilder() {
    }

    public void setParent(Composite parent) {
        this.parent = parent;
    }

    public TableViewer build() {
        TableViewer viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
        Table table = viewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableLayout layout = new TableLayout();
        table.setLayout(layout);

        for (int i = 0; i < columnTexts.length; i++) {
            new TableColumn(table, SWT.NONE).setText(columnTexts[i]);
            layout.addColumnData(new ColumnWeightData(columnWeights[i]));
        }

        viewer.setUseHashlookup(true);
        viewer.setContentProvider(new ArrayContentProvider());
        viewer.setLabelProvider(new LabelProvider());

        return viewer;
    }

    private class LabelProvider implements ITableLabelProvider {
        private Image breakpointImage = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_BREAKPOINT);
        private Image resumeImage = DebugUITools.getImage(IDebugUIConstants.IMG_ACT_RUN);
        
        public LabelProvider() {
        }
        
        public Image getColumnImage(Object element, int columnIndex) {
            Image result = null;
            if (columnIndex == 0) {
                if (element instanceof ResumeEvent) {
                    result = resumeImage;
                } else if (element instanceof SuspendEvent){
                    result = breakpointImage;
                }
            }

            return result;
        }

        public String getColumnText(Object element, int columnIndex) {
            IEvent event = (IEvent) element;
            String result = null;

            switch (columnIndex) {
                case 0:
                    result = event.getTypeDisplay();
                    break;
                case 1:
                    result = event.getDisplay();
                    break;
            }

            return result;
        }

        public void addListener(ILabelProviderListener listener) {

        }

        public void dispose() {
        }

        public boolean isLabelProperty(Object element, String property) {
            return true;
        }

        public void removeListener(ILabelProviderListener listener) {

        }
    }
}